{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RangeIndex(start=0, stop=146, step=1)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    }
   ],
   "source": [
    "#will return a new DataFrame that is indexed by the values in the specified column \n",
    "#and will drop that column from the DataFrame\n",
    "#without the FILM column dropped \n",
    "fandango = pd.read_csv('fandango_score_comparison.csv')\n",
    "print type(fandango)\n",
    "fandango_films = fandango.set_index('FILM', drop=False)\n",
    "#print(fandango_films.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>FILM</th>\n",
       "      <th>RottenTomatoes</th>\n",
       "      <th>RottenTomatoes_User</th>\n",
       "      <th>Metacritic</th>\n",
       "      <th>Metacritic_User</th>\n",
       "      <th>IMDB</th>\n",
       "      <th>Fandango_Stars</th>\n",
       "      <th>Fandango_Ratingvalue</th>\n",
       "      <th>RT_norm</th>\n",
       "      <th>RT_user_norm</th>\n",
       "      <th>...</th>\n",
       "      <th>IMDB_norm</th>\n",
       "      <th>RT_norm_round</th>\n",
       "      <th>RT_user_norm_round</th>\n",
       "      <th>Metacritic_norm_round</th>\n",
       "      <th>Metacritic_user_norm_round</th>\n",
       "      <th>IMDB_norm_round</th>\n",
       "      <th>Metacritic_user_vote_count</th>\n",
       "      <th>IMDB_user_vote_count</th>\n",
       "      <th>Fandango_votes</th>\n",
       "      <th>Fandango_Difference</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>FILM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Kumiko, The Treasure Hunter (2015)</th>\n",
       "      <td>Kumiko, The Treasure Hunter (2015)</td>\n",
       "      <td>87</td>\n",
       "      <td>63</td>\n",
       "      <td>68</td>\n",
       "      <td>6.4</td>\n",
       "      <td>6.7</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.5</td>\n",
       "      <td>4.35</td>\n",
       "      <td>3.15</td>\n",
       "      <td>...</td>\n",
       "      <td>3.35</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.5</td>\n",
       "      <td>19</td>\n",
       "      <td>5289</td>\n",
       "      <td>41</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Do You Believe? (2015)</th>\n",
       "      <td>Do You Believe? (2015)</td>\n",
       "      <td>18</td>\n",
       "      <td>84</td>\n",
       "      <td>22</td>\n",
       "      <td>4.7</td>\n",
       "      <td>5.4</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>0.90</td>\n",
       "      <td>4.20</td>\n",
       "      <td>...</td>\n",
       "      <td>2.70</td>\n",
       "      <td>1.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2.5</td>\n",
       "      <td>31</td>\n",
       "      <td>3136</td>\n",
       "      <td>1793</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ant-Man (2015)</th>\n",
       "      <td>Ant-Man (2015)</td>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>64</td>\n",
       "      <td>8.1</td>\n",
       "      <td>7.8</td>\n",
       "      <td>5.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>4.00</td>\n",
       "      <td>4.50</td>\n",
       "      <td>...</td>\n",
       "      <td>3.90</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>627</td>\n",
       "      <td>103660</td>\n",
       "      <td>12055</td>\n",
       "      <td>0.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>3 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                  FILM  \\\n",
       "FILM                                                                     \n",
       "Kumiko, The Treasure Hunter (2015)  Kumiko, The Treasure Hunter (2015)   \n",
       "Do You Believe? (2015)                          Do You Believe? (2015)   \n",
       "Ant-Man (2015)                                          Ant-Man (2015)   \n",
       "\n",
       "                                    RottenTomatoes  RottenTomatoes_User  \\\n",
       "FILM                                                                      \n",
       "Kumiko, The Treasure Hunter (2015)              87                   63   \n",
       "Do You Believe? (2015)                          18                   84   \n",
       "Ant-Man (2015)                                  80                   90   \n",
       "\n",
       "                                    Metacritic  Metacritic_User  IMDB  \\\n",
       "FILM                                                                    \n",
       "Kumiko, The Treasure Hunter (2015)          68              6.4   6.7   \n",
       "Do You Believe? (2015)                      22              4.7   5.4   \n",
       "Ant-Man (2015)                              64              8.1   7.8   \n",
       "\n",
       "                                    Fandango_Stars  Fandango_Ratingvalue  \\\n",
       "FILM                                                                       \n",
       "Kumiko, The Treasure Hunter (2015)             3.5                   3.5   \n",
       "Do You Believe? (2015)                         5.0                   4.5   \n",
       "Ant-Man (2015)                                 5.0                   4.5   \n",
       "\n",
       "                                    RT_norm  RT_user_norm  \\\n",
       "FILM                                                        \n",
       "Kumiko, The Treasure Hunter (2015)     4.35          3.15   \n",
       "Do You Believe? (2015)                 0.90          4.20   \n",
       "Ant-Man (2015)                         4.00          4.50   \n",
       "\n",
       "                                           ...           IMDB_norm  \\\n",
       "FILM                                       ...                       \n",
       "Kumiko, The Treasure Hunter (2015)         ...                3.35   \n",
       "Do You Believe? (2015)                     ...                2.70   \n",
       "Ant-Man (2015)                             ...                3.90   \n",
       "\n",
       "                                    RT_norm_round  RT_user_norm_round  \\\n",
       "FILM                                                                    \n",
       "Kumiko, The Treasure Hunter (2015)            4.5                 3.0   \n",
       "Do You Believe? (2015)                        1.0                 4.0   \n",
       "Ant-Man (2015)                                4.0                 4.5   \n",
       "\n",
       "                                    Metacritic_norm_round  \\\n",
       "FILM                                                        \n",
       "Kumiko, The Treasure Hunter (2015)                    3.5   \n",
       "Do You Believe? (2015)                                1.0   \n",
       "Ant-Man (2015)                                        3.0   \n",
       "\n",
       "                                    Metacritic_user_norm_round  \\\n",
       "FILM                                                             \n",
       "Kumiko, The Treasure Hunter (2015)                         3.0   \n",
       "Do You Believe? (2015)                                     2.5   \n",
       "Ant-Man (2015)                                             4.0   \n",
       "\n",
       "                                    IMDB_norm_round  \\\n",
       "FILM                                                  \n",
       "Kumiko, The Treasure Hunter (2015)              3.5   \n",
       "Do You Believe? (2015)                          2.5   \n",
       "Ant-Man (2015)                                  4.0   \n",
       "\n",
       "                                    Metacritic_user_vote_count  \\\n",
       "FILM                                                             \n",
       "Kumiko, The Treasure Hunter (2015)                          19   \n",
       "Do You Believe? (2015)                                      31   \n",
       "Ant-Man (2015)                                             627   \n",
       "\n",
       "                                    IMDB_user_vote_count  Fandango_votes  \\\n",
       "FILM                                                                       \n",
       "Kumiko, The Treasure Hunter (2015)                  5289              41   \n",
       "Do You Believe? (2015)                              3136            1793   \n",
       "Ant-Man (2015)                                    103660           12055   \n",
       "\n",
       "                                    Fandango_Difference  \n",
       "FILM                                                     \n",
       "Kumiko, The Treasure Hunter (2015)                  0.0  \n",
       "Do You Believe? (2015)                              0.5  \n",
       "Ant-Man (2015)                                      0.5  \n",
       "\n",
       "[3 rows x 22 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Slice using either bracket notation or loc[]\n",
    "fandango_films[\"Avengers: Age of Ultron (2015)\":\"Hot Tub Time Machine 2 (2015)\"]\n",
    "fandango_films.loc[\"Avengers: Age of Ultron (2015)\":\"Hot Tub Time Machine 2 (2015)\"]\n",
    "\n",
    "# Specific movie\n",
    "fandango_films.loc['Kumiko, The Treasure Hunter (2015)']\n",
    "\n",
    "# Selecting list of movies\n",
    "movies = ['Kumiko, The Treasure Hunter (2015)', 'Do You Believe? (2015)', 'Ant-Man (2015)']\n",
    "fandango_films.loc[movies]\n",
    "\n",
    "#When selecting multiple rows, a DataFrame is returned, \n",
    "#but when selecting an individual row, a Series object is returned instead"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metacritic_User               1.505529\n",
      "IMDB                          0.955447\n",
      "Fandango_Stars                0.538532\n",
      "Fandango_Ratingvalue          0.501106\n",
      "RT_norm                       1.503265\n",
      "RT_user_norm                  0.997787\n",
      "Metacritic_norm               0.972522\n",
      "Metacritic_user_nom           0.752765\n",
      "IMDB_norm                     0.477723\n",
      "RT_norm_round                 1.509404\n",
      "RT_user_norm_round            1.003559\n",
      "Metacritic_norm_round         0.987561\n",
      "Metacritic_user_norm_round    0.785412\n",
      "IMDB_norm_round               0.501043\n",
      "Fandango_Difference           0.152141\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "#The apply() method in Pandas allows us to specify Python logic\n",
    "#The apply() method requires you to pass in a vectorized operation \n",
    "#that can be applied over each Series object.\n",
    "import numpy as np\n",
    "\n",
    "# returns the data types as a Series\n",
    "types = fandango_films.dtypes\n",
    "#print types\n",
    "# filter data types to just floats, index attributes returns just column names\n",
    "float_columns = types[types.values == 'float64'].index\n",
    "# use bracket notation to filter columns to just float columns\n",
    "float_df = fandango_films[float_columns]\n",
    "#print float_df\n",
    "# `x` is a Series object representing a column\n",
    "deviations = float_df.apply(lambda x: np.std(x))\n",
    "\n",
    "print(deviations)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FILM\n",
       "Avengers: Age of Ultron (2015)                    0.375\n",
       "Cinderella (2015)                                 0.125\n",
       "Ant-Man (2015)                                    0.225\n",
       "Do You Believe? (2015)                            0.925\n",
       "Hot Tub Time Machine 2 (2015)                     0.150\n",
       "The Water Diviner (2015)                          0.150\n",
       "Irrational Man (2015)                             0.575\n",
       "Top Five (2014)                                   0.100\n",
       "Shaun the Sheep Movie (2015)                      0.150\n",
       "Love & Mercy (2015)                               0.050\n",
       "Far From The Madding Crowd (2015)                 0.050\n",
       "Black Sea (2015)                                  0.150\n",
       "Leviathan (2014)                                  0.175\n",
       "Unbroken (2014)                                   0.125\n",
       "The Imitation Game (2014)                         0.250\n",
       "Taken 3 (2015)                                    0.000\n",
       "Ted 2 (2015)                                      0.175\n",
       "Southpaw (2015)                                   0.050\n",
       "Night at the Museum: Secret of the Tomb (2014)    0.000\n",
       "Pixels (2015)                                     0.025\n",
       "McFarland, USA (2015)                             0.425\n",
       "Insidious: Chapter 3 (2015)                       0.325\n",
       "The Man From U.N.C.L.E. (2015)                    0.025\n",
       "Run All Night (2015)                              0.350\n",
       "Trainwreck (2015)                                 0.350\n",
       "Selma (2014)                                      0.375\n",
       "Ex Machina (2015)                                 0.175\n",
       "Still Alice (2015)                                0.175\n",
       "Wild Tales (2014)                                 0.100\n",
       "The End of the Tour (2015)                        0.350\n",
       "                                                  ...  \n",
       "Clouds of Sils Maria (2015)                       0.100\n",
       "Testament of Youth (2015)                         0.000\n",
       "Infinitely Polar Bear (2015)                      0.075\n",
       "Phoenix (2015)                                    0.025\n",
       "The Wolfpack (2015)                               0.075\n",
       "The Stanford Prison Experiment (2015)             0.050\n",
       "Tangerine (2015)                                  0.325\n",
       "Magic Mike XXL (2015)                             0.250\n",
       "Home (2015)                                       0.200\n",
       "The Wedding Ringer (2015)                         0.825\n",
       "Woman in Gold (2015)                              0.225\n",
       "The Last Five Years (2015)                        0.225\n",
       "Mission: Impossible â€“ Rogue Nation (2015)       0.250\n",
       "Amy (2015)                                        0.075\n",
       "Jurassic World (2015)                             0.275\n",
       "Minions (2015)                                    0.125\n",
       "Max (2015)                                        0.350\n",
       "Paul Blart: Mall Cop 2 (2015)                     0.300\n",
       "The Longest Ride (2015)                           0.625\n",
       "The Lazarus Effect (2015)                         0.650\n",
       "The Woman In Black 2 Angel of Death (2015)        0.475\n",
       "Danny Collins (2015)                              0.100\n",
       "Spare Parts (2015)                                0.300\n",
       "Serena (2015)                                     0.700\n",
       "Inside Out (2015)                                 0.025\n",
       "Mr. Holmes (2015)                                 0.025\n",
       "'71 (2015)                                        0.175\n",
       "Two Days, One Night (2014)                        0.250\n",
       "Gett: The Trial of Viviane Amsalem (2015)         0.200\n",
       "Kumiko, The Treasure Hunter (2015)                0.025\n",
       "dtype: float64"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rt_mt_user = float_df[['RT_user_norm', 'Metacritic_user_nom']]\n",
    "rt_mt_user.apply(lambda x: np.std(x), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
